<template>
	<view class="content">
		<image class="logo" src="/static/logo.jpg"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		
		<!-- 测试 -->
		<view v-if="getShowTestEntry" style="margin-top: 50px">
			<view style="margin-top: 20px">
				<button type="primary" @click="onJumpUserInfo">查看用户信息</button>
			</view>
			<view style="margin-top: 20px">
				<button type="warn" @click="onJumpSendSms">发送短信</button>
			</view>
			<view style="margin-top: 20px">
				<button type="primary" @click="onJumpPay">测试支付</button>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { computed } from "vue";
	import { onLoad } from "@dcloudio/uni-app";
	import StrUtil from "@/utils/StrUtils.js";
	import { gzhShare } from "@/utils/WxGzhUtil";
	import { useStore } from "vuex";
	
	const store = useStore();
	
	const title = getApp().globalData.appName;
	const sxf_theater = uniCloud.importObject("sxf-theater");
	
	const getShowTestEntry = computed(() => {
		return getApp().globalData.isShowTestEntry;
	});
	
	onLoad(async(option) => {
		console.log("option", option);
		
		gzhShare({
			isShowMenu: false, // 隐藏菜单
			shareInfo: {}
		});
		
		if (getApp().globalData.isRunBrowser) {
			// 浏览器调测，绕开code获取openid
			console.warn("浏览器调测，没执行微信公众号获取code重定向，打包部署时记得要打开code获取逻辑！！");
			if (getApp().globalData.isShowTestEntry) {
				// 测试模式，welcome页面显示测试入口，所以不跳转
			} else {
				// 正式逻辑
				// 进入首页
				uni.switchTab({
					url: "/pages/ticker/list"
				});
				// uni.switchTab({
				// 	url: "/pages/my/index"
				// });
			}
		} else {
			// 正式逻辑
			let link = window.location.href;
			console.log("link: ", link);
			let code = null;
			if (link.indexOf("code=") == -1) {
				// 没有code，引导打开链接进行授权
				const appId = getApp().globalData.gzhAppId;
				let uri = encodeURIComponent(link);
				// let authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${uri}&response_type=code&scope=snsapi_base&state=sxfgzh#wechat_redirect`;
				let authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=sxfgzh#wechat_redirect`;
				// 引导授权并重定向回当前页面
				window.location.href = authUrl;
				console.log("authUrl: ", authUrl);
			} else {
				// 有code
				try {
					let tmp = link.split("code=")[1];
					code = tmp.split("&")[0];
					console.log("code--->", code);
					// /////
					// // 移除url中的code
					// console.log("pathname 1:", window.location.pathname);
					// console.log("location.href 1:", window.location.href);
					// window.history.replaceState(null, null, window.location.pathname);
					// console.log("location.href 2:", window.location.href);
					// //////
					let gzhOpenId = getApp().globalData.curOpenId;
					if (StrUtil.isEmpty(gzhOpenId)) {
						// gzhOpenId = await sxf_theater.getGzhOpenId({ code: code });
						gzhOpenId = await sxf_theater.getGzhOpenId2({ code: code, shareUserId: option.shareUserId });
						if (!StrUtil.isEmpty(gzhOpenId)) {
							getApp().globalData.curOpenId = gzhOpenId;
						}
					}
					console.log("openid：", gzhOpenId);
					if (gzhOpenId !== undefined) {
						// getApp().globalData.curOpenId = gzhOpenId;
						// 获取用户信息
						let userInfo = await sxf_theater.getUserInfo({ openId: gzhOpenId });
						getApp().globalData.curUserId = userInfo._id;
						getApp().globalData.curUserInfo = userInfo;
						store.commit("setUserInfo", userInfo);
						console.log("userInfo：", userInfo);
					}
					
					if (getApp().globalData.isShowTestEntry) {
						// 测试入口，不处理（点击按钮进入相应测试页面）
					} else {
						// 正式处理数据及页面分发跳转
						// 分享人相关参数
						if (!StrUtil.isEmpty(option.shareUserId)) {
							getApp().globalData.shareUserId = option.shareUserId;
						}
						if (!StrUtil.isEmpty(option.shareOpenId)) {
							getApp().globalData.shareOpenId = option.shareOpenId;
						}
						console.log("shareUserId：", option.shareUserId);
						console.log("shareOpenId：", option.shareOpenId);
						
						// 页面跳转
						if (option.page === "main_page") {
							// 跳转到首页
							if (option.tab === "MainPage") {
								uni.switchTab({
									url: "/pages/ticker/list"
								});
								return;
							}
						}
						
						// 其他情况，直接跳转到首页
						uni.switchTab({
							url: "/pages/ticker/list"
						});
					}
				} catch(ex) {
					console.error(ex);
				}
			}
		}
	});
	
	// 测试
	const onJumpUserInfo = () => {
		uni.navigateTo({
			url: "/pages/test/userInfo/index" + "?openId=" + getApp().globalData.curOpenId
		});
	};
	
	const onJumpSendSms = () => {
		uni.navigateTo({
			url: "/pages/test/sms/index" + "?openId=" + getApp().globalData.curOpenId
		});
	};
	
	const onJumpPay = () => {
		uni.navigateTo({
			url: "/pages/test/pay/index" + "?openId=" + getApp().globalData.curOpenId
		});
	};
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 30rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 20rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	
	.desc {
		font-size: 26rpx;
		color: #8f8f94;
	}
</style>